package resource.manager;

import java.util.List;

import resource.manager.classes.DatabaseManager;
import resource.manager.classes.RuntimeManager;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class ResourcesManager extends Activity {

	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        new Thread(new Runnable() {
            public void run() {
            	startService(new Intent(ResourcesManager.this, RuntimeDatabaseService.class));
            	//bindService(new Intent(ResourcesManager.this, 
                  //      RuntimeDatabaseService.class), mConnection, Context.BIND_AUTO_CREATE);
            }
        }).start();
        
        Button buttonLoadTrafficStats = (Button)findViewById(R.id.loadtrafficstats);
        buttonLoadTrafficStats.setOnClickListener(new Button.OnClickListener(){
			public void onClick(View v) {
				Intent intent = new Intent(ResourcesManager.this, TrafficInformation.class);
		        startActivity(intent);
			}
		});

        Button buttonCPUManager = (Button)findViewById(R.id.buttonCPUManager);
        buttonCPUManager.setOnClickListener(new Button.OnClickListener(){
			public void onClick(View v) {
				Intent intent = new Intent(ResourcesManager.this, ApplicationCPUChart.class);
			         startActivity(intent);
			}
		});
        
        Button buttonCPURuntime = (Button)findViewById(R.id.buttonCPURuntime);
        buttonCPURuntime.setOnClickListener(new Button.OnClickListener(){
			public void onClick(View v) {
				RuntimeManager rm;
		        try{
		        	rm = new RuntimeManager();
		            String output = rm.execCommand("");
		            Toast.makeText(ResourcesManager.this, output, Toast.LENGTH_SHORT).show();
		            Log.i("log runtime", output);
		        } catch (Exception ex){
		        	Toast.makeText(ResourcesManager.this, "Error on runtime test", Toast.LENGTH_SHORT).show();
		        }
			}
		});
        
        Button buttonDatabaseTest = (Button)findViewById(R.id.buttonDatabaseTest);
        buttonDatabaseTest.setOnClickListener(new Button.OnClickListener(){
			public void onClick(View v) {
				DatabaseManager dm;
		        try{
		        	dm = new DatabaseManager(ResourcesManager.this);
		        	dm.setInsertCommand("insert into table1 (name) values (?)");
		        	dm.setTableName("table1");
		            dm.insert("Ancuta_testing");
		            List<String> names = dm.selectAll();
			        StringBuilder sb = new StringBuilder();
			        sb.append("Names in database:\n");
			        for (String name : names) {
			        sb.append(name + "\n");
			        }
			        String output = sb.toString();
		            Toast.makeText(ResourcesManager.this, output, Toast.LENGTH_SHORT).show();
		        } catch (Exception ex){
		        	Toast.makeText(ResourcesManager.this, "Error on database test", Toast.LENGTH_SHORT).show();
		        }
			}
		});
        
        }
}